package com.enqualcomm.kids.activities;

import android.content.Intent;
import android.os.Build;
import android.support.v4.util.SimpleArrayMap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.mvp.userinfo.MyAdapter;
import com.enqualcomm.kids.mvp.userinfo.OnItemClickListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;


@EActivity(R.layout.activity_choose_city)
public class ChooseCityActivity extends BaseActivity implements OnItemClickListener {

    @ViewById(R.id.recyclerView)
    RecyclerView recyclerView;
    private MyAdapter mAdapter;
    private String[] province;
    private String[] city;

    private String choosedProvince;
    private String choosedCity;
    private SimpleArrayMap<String, Integer>  map;

    @AfterViews
    void setupViews() {
        TextView title_bar_title_tv = (TextView) findViewById(R.id.title_bar_title_tv);
        title_bar_title_tv.setText(getString(R.string.choose_city));
        initData();
        findViewById(R.id.title_bar_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                processBackMotion();
            }
        });

        recyclerView.setHasFixedSize(true);//固定尺寸，不随内容改动而变化,可以提高性能
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//默认竖直滚动，可以不指定
        recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MyAdapter(LayoutInflater.from(this), this);
        recyclerView.setAdapter(mAdapter);

        filledProvince();
    }

    private void initData() {
        map = new SimpleArrayMap<String, Integer>() {
            {
                put(getString(R.string.huian), R.array.anhui);
                put(getString(R.string.fujian), R.array.fujian);
                put(getString(R.string.gansu), R.array.gansu);
                put(getString(R.string.guangdong), R.array.guangdong);
                put(getString(R.string.guangxi), R.array.guangxi);
                put(getString(R.string.guizhou), R.array.guizhou);
                put(getString(R.string.hainan), R.array.hainan);
                put(getString(R.string.hebei), R.array.hebei);
                put(getString(R.string.henan), R.array.henan);
                put(getString(R.string.heilongjiang), R.array.heilongjiang);
                put(getString(R.string.hubei), R.array.hubei);
                put(getString(R.string.hunan), R.array.hunan);
                put(getString(R.string.jilin), R.array.jilin);
                put(getString(R.string.jiangsu), R.array.jiangsu);
                put(getString(R.string.jiangxi), R.array.jiangxi);
                put(getString(R.string.liaoning), R.array.liaoning);
                put(getString(R.string.neimeng), R.array.neimeng);
                put(getString(R.string.ningxia), R.array.ningxia);
                put(getString(R.string.qinghai), R.array.qinghai);
                put(getString(R.string.shandong), R.array.shandong);
                put(getString(R.string.sanxi), R.array.sanxi);
                put(getString(R.string.shanxi), R.array.shanxi);
                put(getString(R.string.sichuan), R.array.sichuan);
                put(getString(R.string.xizhang), R.array.xizhang);
                put(getString(R.string.xinjiang), R.array.xinjiang);
                put(getString(R.string.yunnan), R.array.yunnan);
                put(getString(R.string.zhejiang), R.array.zhejiang);
            }
        };
    }

    private void filledProvince() {
        province = getResources().getStringArray(R.array.province);
        mAdapter.changeData(province);
        recyclerView.scrollTo(0, 0);
    }

    private void filledCity(int id) {
        city = getResources().getStringArray(id);
        mAdapter.changeData(city);
        recyclerView.scrollTo(0, 0);
    }

    @Override
    public void onClick(View v, int position) {
        if (choosedProvince == null) {
            //选择的是省
//            MyLogger.jLog().i("选中的是："+province[position]);
            //如果有是直辖市，直接返回，否则继续选择城市
            choosedProvince = province[position];
            Integer integer = map.get(choosedProvince);
            if (integer == null) {
                setResultAndFinish();
            } else {
                filledCity(integer);
            }
        } else {
            //选择的是城市
            choosedCity = city[position];
            setResultAndFinish();
        }
    }

    private void setResultAndFinish() {
        Intent intent = new Intent();
        intent.putExtra("province", choosedProvince);
        intent.putExtra("city", choosedCity);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            processBackMotion();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void processBackMotion() {
        if (city != null && choosedProvince != null) {//已经选过省，开始选城市的时候，点击后退
            choosedProvince = null;
            mAdapter.changeData(province);
            recyclerView.scrollTo(0, 0);
        } else {
            finish();
        }
    }

}